/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import blueprint4j.utils.*;

import java.math.*;
import java.text.*;
import javax.swing.*;

public class DataPairFieldBigDecimal extends DataPairDefault implements FieldBinding {
    private BindBigDecimalInterface field;
    private JFormattedTextField text_field;

    public DataPairFieldBigDecimal(Binder binder,BindBigDecimalInterface p_field) {
        this(binder,p_field, true);
    }
    
    public DataPairFieldBigDecimal(Binder binder,BindBigDecimalInterface p_field, boolean editable) {
        super(binder,p_field);
        
        StringBuffer format=new StringBuffer();
        int size=2;
        for (int t=p_field.getUnits()-1;t>0;t--){
        	format.append("#");
        	size++;
        	if (t%3==0){
            	format.append(",");
            	size++;
        	}
        }
    	format.append("0");
    	if (p_field.getFraction()>0){
    		size++;
        	format.append(".");
        	for (int t=0;t<p_field.getFraction();t++){
        		format.append("0");
        	}
    	}
    	
        text_field = new JFormattedTextField(new DecimalFormat(format.toString()+";-"+format.toString()));
        
        text_field.setColumns(size);
        text_field.setHorizontalAlignment(JTextField.RIGHT);
        
        field = p_field;
        setLabel(new JLabel(field.getDescription()));
        setComponent(text_field);
        text_field.setEditable(editable);
    }
    
    public boolean isChanged() {
        BigDecimal value = field.get();
        if (value == null) {
            return text_field.getText() != null;
        } else {
            return text_field.getText() != null && !value.equals(new BigDecimal(text_field.getText()));
        }
    }
    
    public void intoControl() {
        if (field.get() == null) {
            //text_field.setValue(new BigDecimal("0"));
            text_field.setValue(null);
        } else {
        	BigDecimal value=field.get();
            if (value != null) {
                text_field.setValue(value);//new BigDecimal(value.replaceAll(",","")));
            }else{
                text_field.setValue(new BigDecimal("0"));
            }
        }
    }
    
    public void intoBindable() {
        String value=text_field.getText();
        if (value != null && value.length() > 0) {
            field.set(new BigDecimal(value.replaceAll(",","")));
        } else {
            field.set(null);
        }
    }
    
    public Bindable getBindable() {
        return field.getBindable();
    }
    
    public BindBigDecimalInterface getBindField() {
        return (BindBigDecimalInterface) field;
    }
    
    public void setBindField(BindFieldInterface p_field) {
        field = (BindBigDecimalInterface) p_field;
    }
    
    public void resetControl() {
        text_field.setText("");
    }
}

